package java_class_work.week08.internet.sendIntDemo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Author Tiam
 * @Date 2022/11/4 14:07
 * @Description:
 */
public class Send1 {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost", 8888);

            OutputStream outputStream = socket.getOutputStream();
            InputStream inputStream = socket.getInputStream();

            Scanner scanner = new Scanner(System.in);
            while (true){
                /**
                 * read() 只能读取一字节, 范围-128 ~ 127
                 * 使用 byte类型传值刚好.
                 */
                // 先发送指令
                System.out.print("请输入order:");
                byte order = scanner.nextByte();
                scanner.nextLine();
                outputStream.write(order);
                // 读取返回的指令
                byte read = (byte)inputStream.read();
                System.out.println("服务端返回的指令: "+read);

                // 再发送消息
                System.out.print("请输入message:");
                String message = scanner.nextLine();
                outputStream.write(message.getBytes());

                byte[] bytes = new byte[1024];
                int read1 = inputStream.read(bytes);
                System.out.println("返回的消息:"+new String(bytes,0,read1));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
